<template>
  <div>
    <!-- :http-request="onUpload" 自定义上传 -->
    <!-- :file-list="fileList" 上传的文件列表 -->
    <!-- :on-preview="onPreview" 图片预览 -->
    <!-- :on-remove="onRemove" 图片删除 -->
    <!-- :before-upload="beforeUpload" 文件上传之前 -->
    <!-- :limit="limit" 允许上传文件的个数 -->
    <!-- :on-exceed="onExceed" 超出文件个数时的钩子 -->
    <el-upload
      class="avatar-uploader"
      action="#"
      :http-request="onUpload"
      :file-list="fileList"
      :on-preview="onPreview"
      :on-remove="onRemove"
      :before-upload="beforeUpload"
      :on-change="onChange"
      :on-exceed="onExceed"
      :limit="limit"
      list-type="picture-card"
    >
      <!-- <i v-if="!showProgress" class="el-icon-plus avatar-uploader-icon" /> -->
      <span>上传图片</span>
      <!-- 进度条 -->
      <el-progress v-if="showProgress" type="circle" :percentage="percentage" />
      <!-- <div slot="tip" class="el-upload__tip">只能上传{{ limit === 0 ? '任意' : limit }}张.jpg文件，且大小不超过1MB</div> -->
    </el-upload>
    <!-- 预览弹窗 -->
    <el-dialog
      :visible.sync="dialogVisible"
      title="预览"
      width="50%"
    >
      <img :src="item" alt="" style="width:100%;">
    </el-dialog>
  </div>

</template>

<script>
import COS from 'cos-js-sdk-v5'
var cos = new COS({
  SecretId: 'AKIDRhhQs3xaw2qNdcKlumOjriIbdOr51g4C', // 身份识别 ID
  SecretKey: 'sTnreAOL1QYPzIJ3XjeIekZ7OO7ZkfJf' // 身份密钥
})
console.log(cos)
export default {
  name: 'UploadImgae',
  props: {
    limit: {
      type: Number,
      default: () => 0
    },
    code: {
      type: String
    },
    item: {
      type: String
    },
    options: {
      type: Array
    }
  },
  data () {
    return {
      // 上传文件列表
      fileList: [],
      // 预览弹窗开启关闭
      dialogVisible: false,
      // 预览弹窗地址
      imgUrl: '',
      // 进度条的显示
      showProgress: false,
      // 进度条进度
      percentage: 1
    }
  },

  created () {

  },
  watch: {
    item: function (newVal) {
      this.fileList.push({
        uid: 10,
        url: newVal
      })
    }
  },

  methods: {
    // 上传图片
    onUpload (data) {
      // 进度条
      // this.showProgress = true
      // this.id = setInterval(() => {
      //   if (this.percentage < 97) {
      //     this.percentage++
      //   } else {
      //     clearInterval(this.id)
      //   }
      // }, 200)
      cos.putObject({
        Bucket: 'hrsaas-1308960992', // 存储桶
        Region: 'ap-nanjing', // 地域
        Key: data.file.name, // 文件名
        Body: data.file, // 要上传的文件对象
        StorageClass: 'STANDARD' // 上传的模式类型 直接默认 标准模式即可
        // 进度条
        // onProgress: (data) => {
        //   if (data.percent === 1) {
        //     this.percentage = 100
        //     clearInterval(this.id)
        //     setTimeout(() => {
        //       this.showProgress = false
        //       this.percentage = 0
        //     }, 500)
        //   }
        // }
      }, (err, data) => {
        // data返回数据之后 应该如何处理
        console.log(err || data)
        if (err) {
          // this.isShowProgress = false
          this.$message.error('网络错误,上传失败')
          this.fileList.splice(this.fileList.length - 1, 1)
        }
        if (!err && data.statusCode === 200) {
          this.$message.success('图片上传成功')
          const lastUrl = this.fileList.length - 1
          this.fileList[lastUrl].url = 'https://' + data.Location
          this.fileList[lastUrl].code = this.code
          this.$emit('fileList', this.fileList)
        }
        this.fileList = []
      })
    },
    // 预览图片
    onPreview (file) {
      this.imgUrl = file.url
      this.dialogVisible = true
    },
    // 图片删除
    // fileList 删除以后的文件列表
    onRemove (file, fileList) {
      this.fileList = ''
      console.log(1)
      console.log(file)
      console.log(fileList)
      this.$emit('delImg', '')
    },
    // 图片上传之前校验
    beforeUpload (file) {
      // 图片规格限制
      const imageType = ['image/jpeg']
      if (!imageType.includes(file.type)) {
        this.$message.error('请上传规定格式的图片')
        return false
      }
      // 图片大小限制
      const maxSize = 1 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.error('图片大小应在1MB以内')
        return false
      }
    },
    // 成功或失败之后储存filelist
    onChange (file, fileList) {
      this.fileList = fileList
    },
    // 文件上传超出限制
    onExceed () {
      this.$message.error(`最多只能上传${this.limit}张图片`)
    }

  }
}
</script>

<style lang='scss'>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    line-height: 40px;
    text-align: center;
  }
  .el-upload-list--picture-card .el-upload-list__item {
    width: 150px;
    height: 85px;
    margin-left: 10px;
  }
</style>
